<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="zh-cn" style="height: 100%">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
    <title><?=VIEW_TITLE?></title>
    <link href="<?php echo base_url('/assets/bs/'); ?>css/bootstrap.min.css" rel="stylesheet">
    <link href="<?php echo base_url('/assets/css/'); ?>my_common.css?v=<?php echo time(); ?>" rel="stylesheet">
    <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
</head>

<body>
<?php
$this->load->view('admin_header.php');
?>
<div class="container">
    <div id="main_div" style="max-width: 720px;margin: auto;padding-bottom: 100px;">
        <div class="panel panel-default">
            <div class="panel-heading" id="gonggao"><?php echo $gonggao; ?></div>
            <div class="panel-body" style="background-color: #f5f5f5;">
                <div id="scroll_div" style="overflow-y: auto;overflow-x: hidden;">
                    <div id="task_div">

                    </div>
                </div>
                <div>
                    <nav style="text-align: center;margin-top: 0;margin-bottom: 120px;">
                        <ul class="pagination" id="page_ul">
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="tiaokuan_model">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title">《翻译条令》</h4>
            </div>
            <div class="modal-body">
                <div style="overflow-y: scroll;max-height: 400px;">
                    <?php $this->load->view('qiangzhitiaoling.php');?>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="queRenTiaoKuan()">已经阅读并同意</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="hidden-xs">
<?php
$this->load->view('footer.php');
?>
</div>
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="<?php echo base_url('/assets/bs/'); ?>js/bootstrap.min.js"></script>
<script src="<?php echo base_url('/assets/js/'); ?>yy_header_control.js?v=<?php echo time(); ?>"></script>
<script>
    var w_width = document.documentElement.clientWidth;
    var w_height = document.documentElement.clientHeight;
    var scroll_div_height = w_height - 320;
//  $('#scroll_div').height(scroll_div_height + 'px');
    var this_page = 1;
    var this_rows = 10;
    var this_page_sum = 0;
    var user_type = <?php echo $user_type;?>;
    var read_url = "<?php echo site_url('/admin/readOriginalArticleByManagerId?manager_id=')?>";
    var isTheFristReceiveTask = <?php echo isset($isTheFristReceiveTask) ? $isTheFristReceiveTask : 0;?>;
    var queRenTiaoKuan_manage_id = 0;

    function queRenTiaoKuan() {
        $("#tiaokuan_model").modal('hide');
        isTheFristReceiveTask = 0;
        received(queRenTiaoKuan_manage_id);
        queRenTiaoKuan_manage_id = 0;

    }

    function received(manager_id) {
    		var confAccept=confirm('确认领取?')
    		if(!confAccept)return false;
    	
        if (isTheFristReceiveTask == 1) {
            queRenTiaoKuan_manage_id = manager_id;
            $("#tiaokuan_model").modal('show');
            return false;
        }
        $.ajax({
            url: "<?php echo site_url('Translators/receiveTask');?>",
            type: 'post',
            dataType: 'json',
            data: {
                user_id: <?php echo $user_id?>,
                manager_id: manager_id
            },
            success: function (data) {
                if (data == 'success') {
                    $('#receive_btn_' + manager_id).text('已被领取');
                    $('#receive_btn_' + manager_id).attr('disabled', 'disabled');
                    if (typeof getHeaderUserJson === 'function') {
                       // getHeaderUserJson();  //此处是获取头部倒计时功能的，直接跳转过去还会获取一次
                        window.location.href = "<?php echo site_url('admin/translation')?>" + "?manager_id=" + manager_id;
                       
                    } else {
                        console.log('getHeaderUserJson is not exist!');
                    }
                } else {
                    alert(data);
                }
                console.log(data);
            }
        });
    }
    function getTaskJson() {
        $.ajax({
            url: "<?php echo site_url('admin/getTaskJson');?>",
            type: 'post',
            dataType: 'json',
            data: {
                page: this_page,
                rows: this_rows
            },
            success: function (data) {
                var length = data['list'].length;
                var html = '';
                for (var i = 0; i < length; i++) {
                    html += '<div style="position: relative;" class="panel panel-default">';
                    if (user_type == 2) {
                        html += '<span class="badge" style="position: absolute;top:10px;left:10px;">' + data['list'][i]['id'] + '</span>';
                    }
                    html += '<div class="panel-body" style="padding-bottom: 5px;margin-top:15px;">';
                    html += '<div style="text-align: center">' + data['list'][i]['title'] + '</div>';
//                    if (data['list'][i]['yunying_title'] && data['list'][i]['yunying_title'] != null && data['list'][i]['yunying_title'] != '') {
//                        html += '<div style="text-align:center;font-size: 10px;">运管题目:' + data['list'][i]['yunying_title'] + '</div>'
//                    }
                    html += '<div style="float:left;text-align: center;margin-top: 10px;width: 49.9%;">';
                    html += '<a target="_blank" type="button" href="' + read_url + data['list'][i]['id'] + '" class="btn btn-default" style="width: 82px;">阅读原文</a></div>';
                    html += '<div style="float:left;text-align: center;margin-top: 10px;width: 49.9%;">';
                    if (data['list'][i]['is_received'] == 0) {
                        html += '<button type="button" class="btn btn-default btn-primary" style="width: 82px;" id="receive_btn_' + data['list'][i]['id'] + '" onclick="received(' + data['list'][i]['id'] + ')">领取 </button>';
                    } else {
                        html += '<button type="button" class="btn btn-default btn-primary" style="width: 82px;" id="receive_btn_' + data['list'][i]['id'] + '" disabled="disabled"> 已被领取 </button>';
                    }
                    html += '</div></div>';
                    html += '<div class="panel-heading" style="text-align: right;background-color: white;font-size: 12px;">';
                    html += '<span style="display: block;float: left">' + '任务发布时间:' + data['list'][i]['oa_creat_time'] + '</span>';
                    html += '<span style="display: block;float: left"></span>';
                    html += '<span style="display: block;">by:' + data['list'][i]['oa_name'] + '</span>';
                    if (data['list'][i]['translation_article_id'] != 0) {
                        html += '<span style="display: block;">to:' + data['list'][i]['ta_name'] + '</span>';
                    }
                    html += '</div>';
                    html += '</div>';
                }
                $('#task_div').empty();
                $('#task_div').append(html);
                buildPageUl(data['count']);
                //设置cookie
                if (typeof setCookie === 'function') {
                    setCookie('admin_task_page', this_page, 'h24');
                } else {
                    console.log('setCookie is not exist!');
                }
//                buildPageUl(600);
            }
        });
    }

    function getPage(page) {
        this_page = page;
        getTaskJson();
    }

    function nextPage() {
        if (this_page < this_page_sum) {
            ++this_page;
        }
        getPage(this_page);
    }
    function lastPage() {
        if (this_page != 1) {
            --this_page;
        }
        getPage(this_page);
    }
    var pagination_show_num = 7;//每一页显示的页码数量
    function pagination_col(page_num) {
        var begin = 0;
        var finish = 0;
        if (page_num > pagination_show_num) {//需要显示更多了
//            console.log(this_page % pagination_show_num);
            if (this_page != 0 && this_page % pagination_show_num == 0) {//要切换分页组了
                begin = this_page;
                finish = (this_page * 1 + pagination_show_num) * 1;

            } else {
                finish = Math.ceil(this_page / pagination_show_num) * pagination_show_num;
                begin = finish - 7;
            }
            if(finish > page_num){
                finish = page_num;
            }
            //判断是否还有下一组
            if ((finish - begin) < 7){
                $("#shenglue_page").hide();
            }
            for (var i = begin; i <= finish; i++) {
                $('#pagination_' + i).show();
            }
        } else {
            for (var i = 1; i <= 7; i++) {
                $('#pagination_' + i).show();
            }
        }
    }
    function buildPageUl(count_all) {
        $('#page_ul').empty();
        var page_num = Math.ceil(count_all / this_rows);
        this_page_sum = page_num;
        var html = '';
        html += '<li><a href="javascript:getPage(1)">&laquo;</a></li><li class=""><a href="javascript:lastPage()">上一页</a></li>';
        for (var i = 0; i < page_num; i++) {
            if (this_page == (i + 1)) {
                html += '<li style="display:none" id="' + "pagination_" + (i + 1) + '" class="active"><a href="javascript:getPage(' + (i + 1) + ')">' + (i + 1) + '</a></li>';
            } else {
                html += '<li style="display:none" id="' + "pagination_" + (i + 1) + '" ><a href="javascript:getPage(' + (i + 1) + ')">' + (i + 1) + '</a></li>';
            }
        }
        if (pagination_show_num < page_num) {
            html += '<li class="" id="shenglue_page"><a href="javascript:nextPage()">...</a></li>';
        }
        html += '<li class=""><a href="javascript:nextPage()">下一页</a></li><li><a href="javascript:getPage(' + page_num + ')">&raquo;</a></li>';
        $('#page_ul').append(html);
        pagination_col(page_num);
    }


    function read(manager_id) {
        window.location.href = "<?php echo site_url('/admin/readOriginalArticleByManagerId?manager_id=')?>" + manager_id;
    }

    if (typeof getCookie === 'function') {
        var cookie_page = getCookie('admin_task_page');
        if (cookie_page != '' && !isNaN(cookie_page)) {
            this_page = cookie_page;
        }
    } else {
        console.log('getCookie is not exist!');
    }
    getPage(this_page);

</script>
</body>
</html>